(function($){

    $.fn.extend({
        serviceDropdown : function(options, arg) {
            if (typeof(options) == "object") {
                options = $.extend({}, $.serviceDropdown.defaults, options);
            }
            this.each(function() {
                for (var fld in options) {
                    jQuery(this).attr("plugin_sd_" + fld, options[fld]);
                }
                new $.serviceDropdown(this, options, arg);
            });
            return;
        }
    });

    $.serviceDropdown = function(item, options, arg) {
        var myName = item.name;
        var pos = jQuery(item).offset();
        var serviceList = jQuery("<select>")
            .attr("id", myName + "_list")
            .attr("size", 6)
            .addClass("servicedropdown")
            .css({
                height : "140px"
            })
            .hide()
            .change(function() {
                jQuery(item).val(jQuery(this).find("option:selected").text());
                jQuery(item).addClass("textentered");
            })
        ;
        jQuery("body").append(serviceList);
        if (typeof(options) == "string") {
            var methodName = options;
            options = {};
            for (var fld in $.serviceDropdown.defaults) {
                options[fld] = jQuery(item).attr("plugin_sd_" + fld);
            }
            // methods
        } else {
            if (jQuery(item).val() == "") {
                jQuery(item).removeClass("textentered");
                jQuery(item).val(options.defaulttext);
            } else {
                jQuery(item).addClass("textentered");
            }
            jQuery(item).click(function() {
                dropdownClicked(options, serviceList, pos);
            });
            jQuery("img[ref='" + item.name + "']").click(function() {
                dropdownClicked(options, serviceList, pos);
            });
            jQuery("img[ref='" + myName + "'][func='search']").click(function() {
                redirect("list_services", "", {
                    selectedService : serviceList.find("option:selected").val()
                }, true);
            });
        }

        $.serviceDropdown.defaults = {
            loader : "getavailable",
            page : "services"
        }
    }

    function dropdownClicked(options, serviceList, pos) {
        callService(options.page, options.loader, {
        }, {
            success : function(result) {
                serviceList.empty();
                for (var i = 0; i < result.items.length; i++) {
                    serviceList.append(jQuery("<option>")
                        .text(result.items[i].text)
                        .val(result.items[i].value)
                    );
                }
                serviceList
                    .css({
                        position : "absolute",
                        top : pos.top + 25,
                        left : pos.left - 5
                    })
                    .show("fade", {}, 300)
                ;
            }
        });
    }
})(jQuery);

jQuery(window).click(function(e) {
    var ev = (e ? e : window.event);
    var element = (ev.target ? ev.target : ev.srcElement);
    if (!jQuery(element).hasClass("servicedropdown")) {
        jQuery("select.servicedropdown").hide("fade", {}, 300);
    }
});
